<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>添加或者修改自定义帖子</title>
<link rel="stylesheet" href="/layui/css/layui.css"></link>
<style type="text/css">

	table tr td{
		padding: 10px;
	}

</style>
</head>
<body>
<div style="padding: 20px">
	<form method="post">
		<table>
			<tr>
				<td>标题：</td>
				<td><input type="text" autocomplete="off" id="title" name="title" class="layui-input" style="width: 360px"/></td>
			</tr>
			<tr>
				<td valign="top">内容：</td>
				<td>
					<textarea type="text" class="ckeditor" id="content" name="content" class="layui-input" ></textarea>
				</td>
			</tr>
			<tr>
				<td><button  class="layui-btn" onclick="submitData();return false;">提交</button></td>
				<td><font id="errorInfo" color="red"></font></td>
			</tr>
		</table>
	</form>
</div>
<script src="/ckeditor/ckeditor.js"></script>
<script src="/layui/layui.js"></script>
<script src="/js/jquery.js"></script>
<script src="/js/common.js"></script>
<script type="text/javascript">

    layui.use(['form'], function(){

    });

    function submitData(){
        var title=$("#title").val().trim();
        var content=CKEDITOR.instances.content.getData();
        if(title=="") {
            $("#errorInfo").text("请输入自定义帖子标题！");
            $("#name").focus();
            return false;
        }
        if(content=="") {
            $("#errorInfo").text("请输入自定义帖子内容！");
            $("#url").focus();
            return false;
        }
        var id=getQueryVariable("id");
        if(id){
            $.post("/admin/article/save",{id:id,title:title,content:content},function(result){
                if(result.success){
                    layer.alert('修改成功！',function () {
                        parent.reloadPage();
                    });

                }else{
                    $("#errorInfo").text(result.errorInfo);
                }
            },"json");
        }else{
            $.post("/admin/article/save",{title:title,content:content},function(result){
                if(result.success){
                    layer.alert('添加成功！',function () {
                        parent.reloadPage();
                    });
                }else{
                    $("#errorInfo").text(result.errorInfo);
                }
            },"json");
        }
    }



    function getQueryVariable(variable){
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i=0;i<vars.length;i++) {
            var pair = vars[i].split("=");
            if(pair[0] == variable){return pair[1];}
        }
        return(false);
    }

    $(function(){

        var id=getQueryVariable("id");

        if(id){
            $.post("/admin/article/findById",{id:id},function(result){
                if(result.success){
                    var article=result.article;
                    $("#title").val(article.title);
                    CKEDITOR.instances.content.setData(article.content);
                }else{
                    layer.alert("服务器加载有问题，请联系管理员！");
                }
            },"json");
        }
    });


</script>
</body>
</html>